package com.sec.etech.ism.util;

import com.alibaba.fastjson.JSONObject;
import com.sec.etech.ism.constant.SignatureConstant;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class SapSignatureUtil {
    static final String SAP_PARAMS = "sap_params";

    /**
     * 构建接口请求体的内容
     * @param sapParams SAP BAPI函数的参数内容
     * @param appSecrete 签名秘钥（指SAP中间件分配的系统秘钥
     * @param timeStamp 时间戳
     * @return 返回调用接口的请求体内容
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws UnsupportedEncodingException
     */
    public static JSONObject buildRequestParamsByTimeStamp(JSONObject sapParams, String appSecrete, Long timeStamp)
            throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
        // 接口请求的输入参数
        JSONObject inputParams = new JSONObject();
        inputParams.put(SAP_PARAMS, sapParams);
        return SignatureUtil.buildRequestParamsByTimeStamp(inputParams, appSecrete, timeStamp, SignatureConstant.SIGNATURE_MAC);
    }

    /**
     * 构建接口请求体的内容，使用系统当前时间作为时间戳进行构建
     * @param sapParams SAP BAPI函数的参数内容
     * @param appSecrete 签名秘钥（指SAP中间件分配的系统秘钥
     * @return 返回调用接口的请求体内容
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws UnsupportedEncodingException
     */
    public static JSONObject buildRequestParams(JSONObject sapParams, String appSecrete) throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
        return buildRequestParamsByTimeStamp(sapParams, appSecrete, System.currentTimeMillis() / 1000);
    }

    /**
     * 测试验证签名方法是否符合预期
     *    时间戳:1629706324
     * 1、需要签名的字符串:{"sap_params":{"COMPANYCODEID":"2802","NAME":"数科","PRNNUM":200}}
     * 1629706324
     * 2、参数签名结果:sS331PN9yfEq8cE/GkEEv9rJtFiwyh/Cexdv5nFNjPY=
     * 3、请求参数格式如下:{"input_params":{"sap_params":{"COMPANYCODEID":"2802","NAME":"数科","PRNNUM":200}},"signature_created":1629706324,"signature":"sS331PN9yfEq8cE/GkEEv9rJtFiwyh/Cexdv5nFNjPY="}
     * @param appSecret
     * @throws Exception
     */
    public static void  test(String appSecret) throws Exception{
        //SAP BAPI 函数对应的参数内容JSON
        JSONObject sapParams = new JSONObject();
        sapParams.put("COMPANYCODEID", "2802");
        sapParams.put("NAME", "数科");
        sapParams.put("PRNNUM", 200);

        buildRequestParamsByTimeStamp(sapParams,appSecret,1629706324L);
    }
    /**
     * 主函数
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        try {
            //上海电气数据交换中间件提供的秘钥
            String appSecret = "324687b1df114179bfe7";

            System.out.printf("\n\n测试验证签名方法是否符合预期：\n\n");
            test(appSecret);

            //SAP BAPI 函数对应的参数内容JSON
            JSONObject sapParams = new JSONObject();
            sapParams.put("COMPANYCODEID", "2802");
            sapParams.put("NAME", "数科");
            sapParams.put("PRNNUM", 200);

            System.out.printf("\n\n使用当前时间作为时间戳，构建请求体的内容：\n\n");
            buildRequestParams(sapParams,appSecret);

        } catch (Exception e) {
            System.out.println("Error");
            e.printStackTrace();
        }
    }
}
